package lc.smart.android.main;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MediaStoreCameraActivity extends Activity

{
	public static final String TAG_STRING = "MediaStoreCameraActivity";//标识
	private Context mContext;

	final static int CAMERA_RESULT = 0;
	Uri imageFileUri;

	//界面元素
	ImageView returnedImageView;
	Button takePictureButton;
	Button saveDataButton;
	TextView titleTextView;
	TextView descriptiongTextView;
	EditText titleEditText;
	EditText descriptionEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mContext = this;
		//将内容视图在main。xml文件中定义的视图
		setContentView(R.layout.activity_media_store_camera);
		//获取UI元素的引用
		returnedImageView = (ImageView) findViewById(R.id.ReturnedImageView);
		takePictureButton = (Button) findViewById(R.id.TakePictureButton);
		saveDataButton = (Button) findViewById(R.id.SaveDataButton);
		titleTextView = (TextView) findViewById(R.id.TitleTextView);
		descriptiongTextView = (TextView) findViewById(R.id.DescriptionTextView);
		titleEditText = (EditText) findViewById(R.id.TitleEditText);
		descriptionEditText = (EditText) findViewById(R.id.DescriptionEditText);

		this.returnedImageView.setVisibility(View.GONE);
		this.saveDataButton.setVisibility(View.GONE);
		this.titleTextView.setVisibility(View.GONE);
		this.descriptiongTextView.setVisibility(View.GONE);
		this.titleEditText.setVisibility(View.GONE);
		this.descriptionEditText.setVisibility(View.GONE);

		//当单击拍照按钮时
		this.takePictureButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				//添加一条不带位图的新记录
				//返回新的记录的Uri
				imageFileUri = getContentResolver().insert(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						new ContentValues());
				//启动camera 应用程序
				//进入系统照相程序
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
						imageFileUri);
				startActivityForResult(intent, CAMERA_RESULT);

			}
		});

		this.saveDataButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				//更新mediaStore中记录的标题和描述
				ContentValues contentValues = new ContentValues(3);
				contentValues.put(MediaStore.Images.Media.DISPLAY_NAME,
						titleEditText.getText().toString());
				contentValues.put(MediaStore.Images.Media.DESCRIPTION,
						titleEditText.getText().toString());
				getContentResolver().update(imageFileUri, contentValues, null,
						null);

				//通知用户
				showMessage("Record Updated");

				//回到初始状态，设置拍照按钮为可见
				//隐藏其他UI元素
				takePictureButton.setVisibility(View.VISIBLE);
				returnedImageView.setVisibility(View.GONE);
				saveDataButton.setVisibility(View.GONE);
				titleTextView.setVisibility(View.GONE);
				descriptiongTextView.setVisibility(View.GONE);
				titleEditText.setVisibility(View.GONE);
				descriptionEditText.setVisibility(View.GONE);

			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent)
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if (SmartApplication.is_DubugMode)
		{
			Log.i(TAG_STRING,
					"uri paht:" + imageFileUri.getPath() + " Authority:"
							+ imageFileUri.getAuthority() + " Query:"
							+ imageFileUri.getEncodedQuery() + " Fragment:"
							+ imageFileUri.getFragment() + " toString:"
							+ imageFileUri.toString());
			if (intent != null)
			{
				Log.i(TAG_STRING, intent.toString());
			}
			else
			{
				Log.i(TAG_STRING, "intent is null!");
			}

		}

		if (resultCode == RESULT_OK)
		{

			takePictureButton.setVisibility(View.GONE);
			returnedImageView.setVisibility(View.VISIBLE);
			saveDataButton.setVisibility(View.VISIBLE);
			titleTextView.setVisibility(View.VISIBLE);
			descriptiongTextView.setVisibility(View.VISIBLE);
			titleEditText.setVisibility(View.VISIBLE);
			descriptionEditText.setVisibility(View.VISIBLE);

			//获取没有保存的返回相片数据
			//Bitmap bmpBitmap = (Bitmap) intent.getExtras().get("data");
			//加载保存图片路径方式一
			//Bitmap bmpBitmap = BitmapFactory.decodeFile(imageFilePathString);
			//使用uri来提取文件路径
			//Bitmap bmpBitmap = BitmapFactory.decodeFile(imageUri.getPath());

			//加载大图片并并房子oom
			//获取屏幕大小
			//Display currentDisplay = getWindowManager().getDefaultDisplay();
			try
			{
				int dw = 200;
				int dh = 200;
				//加载图像尺寸而不是图像本身
				BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
				bmpFactoryOptions.inJustDecodeBounds = true;
				Bitmap bitmap = BitmapFactory
						.decodeStream(
								getContentResolver().openInputStream(
										imageFileUri), null, bmpFactoryOptions);
				int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
						/ (float) dh);
				int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
						/ (float) dw);
				if (SmartApplication.is_DubugMode)
				{
					Log.i(TAG_STRING, "heightRatio:" + heightRatio
							+ " widthRatio:" + widthRatio);

				}
				//如果两个比率都大于1；
				//那么图像的一条边将大于屏幕
				if (heightRatio > 1 && widthRatio > 1)
				{
					if (heightRatio > widthRatio)
					{
						//若高度比率更大，则根据它缩放
						bmpFactoryOptions.inSampleSize = heightRatio;
					}
					else
					{
						//若宽度比率更大，则根据它缩放
						bmpFactoryOptions.inSampleSize = widthRatio;
					}
				}
				//对图像进行真正的解码
				bmpFactoryOptions.inJustDecodeBounds = false;
				bitmap = BitmapFactory
						.decodeStream(
								getContentResolver().openInputStream(
										imageFileUri), null, bmpFactoryOptions);

				returnedImageView.setImageBitmap(bitmap);
			}
			catch (FileNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.v(TAG_STRING, "error:" + e.toString());
			}

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.media_store_camera, menu);
		return true;
	}

	public void showMessage(String msg)
	{
		Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
	}
}
